package cn.alcatraz.org.controller;


import cn.alcatraz.basic.annotation.PreAuthorize;
import cn.alcatraz.basic.utils.AjaxResult;
import cn.alcatraz.basic.utils.PageList;
import cn.alcatraz.org.domain.Employee;
import cn.alcatraz.org.query.EmployeeQuery;
import cn.alcatraz.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/employee")
@Api(value = "员工的API",description="员工相关的CRUD功能")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

   
    @ApiOperation(value = "查询所有" )
    @GetMapping
    @PreAuthorize(name="员工查询所有",sn="employee:loadAll")
    public List<Employee> loadAll(){
        return employeeService.loadAll();
    }

    
    @ApiOperation(value = "通过ID查询" )
    @GetMapping("/{id}")
    @PreAuthorize(name="员工根据ID查询",sn="employee:loadById")
    public Employee loadById(@ApiParam(value="员工表的主键",required = true) @PathVariable("id") Long id){
        return employeeService.loadById(id);
    }
   
    @ApiOperation(value = "通过ID判断新增或修改" )
    @PutMapping
    @PreAuthorize(name="员工新增修改",sn="employee:addOrUpt")
    public AjaxResult addOrUpt(@ApiParam(value="员工对象",required = true) @RequestBody Employee employee){
        try{
            if(employee.getId() == null){
                employeeService.save(employee);
            }else {
                employeeService.update(employee);
            }
            return AjaxResult.myself();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.myself().setSuccess(false).setMessage("系统繁忙，请稍后重试！");
        }
    }
    
    @ApiOperation(value = "通过ID删除" )
    @DeleteMapping("/{id}")
    @PreAuthorize(name="员工删除",sn="employee:delete")
    public AjaxResult delete(@ApiParam(value="员工表的主键",required = true) @PathVariable("id") Long id){
        try {
            employeeService.delete(id);
            return AjaxResult.myself();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.myself().setSuccess(false).setMessage("系统繁忙，请稍后重试！");
        }
    }
    
    @ApiOperation(value = "查询分页数据" )
    @PostMapping
    @PreAuthorize(name="员工分页查询",sn="employee:queryData")
    public PageList<Employee> queryData(@ApiParam(value="分页查询对象",required = true)@RequestBody EmployeeQuery employeeQuery){
        PageList<Employee> data = employeeService.queryData(employeeQuery);
        return data;
    }
    /**
    * @Title: batchDelete
    * @Description: 员工批量删除
    * @Author: Alcatraz
    * @Version: 1.0
    * @Date:  2022/7/13 16:27
    * @Parameters: [ids]
    * @Return cn.alcatraz.basic.utils.AjaxResult
    */
    @PatchMapping
    @PreAuthorize(name="员工批量删除",sn="employee:batchDelete")
    public AjaxResult batchDelete(@RequestBody Long[] ids){
        try {
            employeeService.batchDelete(ids);
            return AjaxResult.myself();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.myself().setSuccess(false).setMessage("系统繁忙，请稍后重试！");
        }
    }

}
